/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.base.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.mes.base.entity.ProducePlan;
import org.springblade.modules.mes.base.service.IProducePlanService;
import org.springblade.modules.mes.base.service.SequenceService;
import org.springblade.modules.mes.base.utils.OverlapUtil;
import org.springblade.modules.mes.base.vo.ProducePlanVO;
import org.springblade.modules.mes.opcua.constant.OpcUaCache;
import org.springblade.modules.system.util.QueryParaUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 生产计划 控制器
 *
 * @author BladeX
 * @since 2020-09-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/producePlan")
@Api(value = "生产计划", tags = "生产计划接口")
public class ProducePlanController extends BladeController {

	private final IProducePlanService producePlanService;
	private final SequenceService sequenceService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	//@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入producePlan")
	public R<ProducePlan> detail(ProducePlan producePlan) {
		ProducePlan detail = producePlanService.getOne(Condition.getQueryWrapper(producePlan));
		return R.data(detail);
	}

	/**
	 * 不分页 生产计划
	 */
	@GetMapping("/list")
	//@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页", notes = "传入produceLine")
	public R<List<ProducePlan>> list(ProducePlanVO producePlanVO) {
		ProducePlanVO cond = buildCond(producePlanVO);
		List<ProducePlan> produceLineVOList = producePlanService.list(Condition.getQueryWrapper(cond));
		return R.data(produceLineVOList);
	}


	/**
	 * 自定义分页 生产计划
	 */
	@GetMapping("/page")
	//@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入producePlan")
	public R<IPage<ProducePlanVO>> page(ProducePlanVO producePlanVO, Query query) {
		ProducePlanVO cond = buildCond(producePlanVO);
		IPage<ProducePlanVO> pages = producePlanService.selectProducePlanPage(Condition.getPage(query), cond);
		return R.data(pages);
	}


	/**
	 * 新增 生产计划
	 */
	@PostMapping("/save")
	//@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入producePlan")
	public R save(@Valid @RequestBody ProducePlan producePlan) {
		try {
			producePlan.setId(IdUtil.getSnowflake(1, 2).nextId());
			producePlan.setPlanCode("P"+sequenceService.planNo());
			if(OverlapUtil.overlap(producePlan)){
				return R.fail("流水号重叠，不能保存");
			}
			producePlan.setStatus(0);
			boolean flag = producePlanService.save(producePlan);

			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}


	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入producePlan")
	public R submit(@Valid @RequestBody ProducePlan producePlan) {
		try {
			producePlan.setPlanCode(null);
			if(OverlapUtil.overlap(producePlan)){
				return R.fail("流水号重叠，不能保存");
			}
			producePlan.setStatus(null);
			boolean flag = producePlanService.updateById(producePlan);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/add")
	@ApiOperation(value = "新增", notes = "传入producePlan")
	public R add(@Valid @RequestBody ProducePlan producePlan) {
		try {
			producePlan.setId(IdUtil.getSnowflake(1, 2).nextId());
			producePlan.setPlanCode("P"+sequenceService.planNo());
			if(OverlapUtil.overlap(producePlan)){
				return R.fail("流水号重叠，不能保存");
			}
			producePlan.setStatus(0);
			boolean flag = producePlanService.save(producePlan);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/edit")
	@ApiOperation(value = "新增或修改", notes = "传入producePlan")
	public R edit(@Valid @RequestBody ProducePlan producePlan) {
		try {
			producePlan.setPlanCode(null);
			if(OverlapUtil.overlap(producePlan)){
				return R.fail("流水号重叠，不能保存");
			}
			producePlan.setStatus(null);
			boolean flag = producePlanService.updateById(producePlan);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 删除 生产计划
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		try {
			List<Long> idList = Func.toLongList(ids);
			ProducePlanVO producePlan = new ProducePlanVO();
			for (Long id : idList) {
				producePlan.setId(id);
				producePlanService.deleteById(producePlan);
			}
			return R.status(true);
		} finally {
			OpcUaCache.clear();
		}
	}
	/**
	 * 停止 生产计划
	 */
	@PostMapping("/stop")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R stop(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		try {
			List<Long> idList = Func.toLongList(ids);
			for (Long id : idList) {
				UpdateWrapper<ProducePlan>  updateWrapper = new UpdateWrapper<>();
				updateWrapper.eq("id",id);
				updateWrapper.set("status",15);
				producePlanService.update(updateWrapper);
			}
			return R.status(true);
		} finally {
			OpcUaCache.clear();
		}
	}


	@NotNull
	private ProducePlanVO buildCond(ProducePlanVO producePlanVO) {
		return QueryParaUtil.trim(producePlanVO);
	}

}
